<!--
  * 移位单
  *
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-card size="small" :bordered="false" :hoverable="false" class="query-form-wrapper smart-margin-bottom10 smart-radius-2">
    <a-form :model="queryForm" labelAlign="left">
      <a-row :gutter="[8, 10]">
        <a-col :span="{ sm: 7, md: 6 }">
          <a-form-item label="单据日期" class="smart-margin-bottom0">
            <custom-range-picker v-model:value="queryForm.create_time" />
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="移货单号" class="smart-margin-bottom0">
            <a-input v-model:value.trim="queryForm.no" placeholder="移货单号" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-button-group>
            <a-button type="primary" html-type="submit" @click="onSearch">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true" class="query-table-wrapper">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <!-- <a-button @click="addOrUpdate()" type="primary" v-privilege="'shift:add'">
          <template #icon>
            <PlusOutlined />
          </template>
          新建
        </a-button>

        <a-button @click="onExport" type="primary" v-privilege="'shift:export'">
          <template #icon>
            <ExportOutlined />
          </template>
          导出
        </a-button> -->
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.ERP.GOODS" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->
    <div class="table-wrapper">
      <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :scroll="{ x: 'max-content', y: tableScrollY }" :pagination="false" :showSorterTooltip="false" :loading="tableLoading" :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }">
        <template #bodyCell="{ text, record, column }">
          <template v-if="column.dataIndex === 'no'">
            <CopyText :text="text">
              <a @click="detail(record)">{{ text }}</a>
            </CopyText>
          </template>
          <template v-if="column.dataIndex === 'startWarehouse'">
            <span
              >{{ record.startWarehouse?.name }}<span v-if="record.startWarehouse?.attribute_type">({{ getDescByValue('WAREHOUSE_ATTRIBUTE_TYPE', record.startWarehouse?.attribute_type) }})</span><span v-if="record.startWarehouseLocation">({{ record.startWarehouseLocation?.code }})</span>
            </span>
          </template>
          <template v-if="column.dataIndex === 'endWarehouse'">
            <span
              >{{ record.endWarehouse?.name }}<span v-if="record.endWarehouse?.attribute_type">({{ getDescByValue('WAREHOUSE_ATTRIBUTE_TYPE', record.endWarehouse?.attribute_type) }})</span><span v-if="record.endWarehouseLocation">({{ record.endWarehouseLocation?.code }})</span>
            </span>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <div class="smart-table-operate">
              <a-button @click="detail(record)" type="link" style="color: #97999c">详情</a-button>
            </div>
          </template>
        </template>
      </a-table>
    </div>

    <div class="smart-query-table-page query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS" :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData" :show-total="(total) => `共${total}条`" />
    </div>

    <ShiftFormModal ref="formModal" @reloadList="queryData" />

    <ShiftDetailsModal ref="detailRef" />
  </a-card>
</template>
<script setup lang="ts">
import CustomRangePicker from '@/components/business/custom-range-picker/index.vue'
import ShiftDetailsModal from './components/shift-details-modal.vue'
import ShiftFormModal from './components/shift-form-modal.vue'
import { movingOrderApi } from '@/api/business/storage/shift-api'
import { PAGE_SIZE_OPTIONS, DEFAULT_TIME_RANGE } from '@/constants/system/common-const'
import TableOperator from '@/components/support/table-operator/index.vue'
import { TABLE_ID_CONST } from '@/constants/support/table-id-const'
import { useQuery } from '@/hooks/web/use-query-hook'
import { QUERY_FORM_OP } from '@/constants/support/query-form-op-const'
import { useEnums } from '@/hooks/plugins/useEnums'
import { useAppConfigStore } from '@/store/modules/app-store'
import { useResize } from '@/hooks/web/use-resize-hook'
const appConfigStore = useAppConfigStore()
const { getDescByValue } = useEnums()

// ----------------------------- 计算表格高度 -----------------------------
const { tableScrollY } = useResize()

// ---------------------------- 表格列 ----------------------------

const columns = computed(() => [
  {
    title: '移货单号',
    dataIndex: 'no',
    width: appConfigStore.$state.compactFlag ? 150 : 180
  },
  // {
  //   title: '经销商',
  //   dataIndex: ['dealer', 'name']
  // },
  {
    title: '来源仓库',
    dataIndex: 'startWarehouse'
  },
  {
    title: '目标仓库',
    dataIndex: 'endWarehouse'
  },
  {
    title: '商品数量',
    dataIndex: 'order_number'
  },
  {
    title: '商品货值',
    dataIndex: 'detail_sum'
  },
  {
    title: '移货时间',
    dataIndex: 'create_time'
  },
  {
    title: '制单人',
    dataIndex: ['creator', 'username'],
    width: 100
  },
  {
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    width: appConfigStore.$state.compactFlag ? 100 : 130
  }
])

// ---------------------------- 查询数据表单和方法 ----------------------------
const { queryForm, queryFormOp, tableData, tableLoading, total, resetQuery, onSearch, queryData, deleteData } = useQuery({
  fetchDataApi: async (params) => {
    const res = await movingOrderApi.queryMovingOrderList(params)
    return {
      list: res.data.data,
      total: res.data.total
    }
  },
  queryFormFilter: {
    no: '',
    create_time: DEFAULT_TIME_RANGE.value
  },
  queryFormOp: {
    no: QUERY_FORM_OP['模糊查询'],
    create_time: QUERY_FORM_OP['范围查询']
  }
})

const route = useRoute()
onMounted(() => {
  queryForm.value.create_time = route.query.create_time || route.query?.no ? [] : DEFAULT_TIME_RANGE.value
  queryForm.value.no = route.query.no
  queryData()
})

// ---------------------------- 添加/修改 ----------------------------
const formModal = ref()

function addOrUpdate(data?: any) {
  formModal.value.showDrawer(data)
}

// ---------------------------- 详情 ----------------------------
const detailRef = ref()

const detail = (data) => {
  detailRef.value.showDrawer(data)
}
// ---------------------------- 批量删除 ----------------------------

// 选择表格行
const selectedRowKeyList = ref([])

function onSelectChange(selectedRowKeys) {
  selectedRowKeyList.value = selectedRowKeys
}

// ------------------------------- 导出和导入 ---------------------------------
// 导出
function onExport() {
  const { page, limit, ...filter } = queryForm.value
  const data = {
    filter: JSON.stringify(filter),
    op: JSON.stringify(queryFormOp)
  }
  movingOrderApi.exportXlsx(data)
}
</script>
